package j01die;

public class J1 {
    static Object lock1 = new Object();
    static Object lock2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            synchronized (lock1){
                System.out.println("t1 - enter lock1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock2){
                    System.out.println("t1 - enter lock2");
                    System.out.println("t1 - end lock2");
                }
                System.out.println("t1 - end lock1");
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (lock2){
                System.out.println("t2222 - enter lock2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock1){
                    System.out.println("t2222 - enter lock1");
                    System.out.println("t2222 - end lock1");
                }
                System.out.println("t2222 - end lock2");
            }
        });
        t1.start();
        t2.start();
    }
}

class J2{
    static Object lock = new Object();
    public static void main(String[] args) {
        System.out.println("enter main");
        new Thread(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            //唤醒主线程
            synchronized (lock){
                System.out.println("子线程要唤醒主线程");
                lock.notify();
            }


        }).start();
        Thread main = Thread.currentThread();

        synchronized (lock){
            //程序暂停，等待被lock.notify（）唤醒
            // 打开lock
            //需要编译时异常
            try {
                System.out.println("主线程开始wait...");
                lock.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(1111111);
        }
        System.out.println("main end");
    }
}
